#include "AppDelegate.h"

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate() 
{
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::create("My Game");
        director->setOpenGLView(glview);
    }

    std::vector<std::string> searchPath;
    searchPath.push_back(xhdpi.directory);
    searchPath.push_back("sounds");
    FileUtils::getInstance()->setSearchPaths(searchPath);
    
    // specify vector move which make sure for multi-screen size
    Size size = glview->getFrameSize();
   	Size frameSize = glview->getFrameSize();
    float correctRate = designResolutionSize.height / designResolutionSize.width;
    float frameRate = size.height / size.width;
    float fitWidth = 0;
    float fitHeight = 0;
    if (frameRate > correctRate) {
        fitWidth = designResolutionSize.width;
        fitHeight = designResolutionSize.width * frameRate;
        glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_WIDTH);
    } else {
        fitWidth = designResolutionSize.height / frameRate;
        fitHeight = designResolutionSize.height;
        glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT);
    }
    Point displayResolutionSize = Point(fitWidth, fitHeight);
    Point vectorMove = (displayResolutionSize + (Point)designResolutionSize * (-1)) * 0.5f;
    // load Resources
    SpriteFrameCache *sfc = SpriteFrameCache::getInstance();
    sfc->addSpriteFramesWithFile("menu.plist");
    sfc->addSpriteFramesWithFile("game.plist");

    // initialize game model
    GameModel* gameModel = GameModel::getInstance();
    gameModel->setDisplayResolutionSize((Size)displayResolutionSize);
    gameModel->initialize();
    gameModel->setVectorMove(vectorMove);
    
    // initialize game controller
    GameController* gameController = new GameController();
    this->gameController = gameController;
    this->app = UGApplication::getInstance();
    

    // turn on display FPS
    director->setDisplayStats(false);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);

    // run
    UGEvent* beginGameEvent = UGDefaultEvent::create(REQ_BEGIN_GAME, EVT_DEFAULT, NULL, 0, NULL);
    this->app->getSharedEventNotifier()->notifyEvent(beginGameEvent);
    Utils::random();
    return true;


    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

extern "C"{
    size_t fwrite$UNIX2003( const void *a, size_t b, size_t c, FILE *d )
    {
        return fwrite(a, b, c, d);
    }
    char* strerror$UNIX2003( int errnum )
    {
        return strerror(errnum);
    }
}
